<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数据字典操作</title>
    <script src="../../../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../../../res/layui/css/layui.css" media="all">
    <link rel="shortcut icon" type="image/x-icon" href="../../../../res/images/favicon.ico">
    <link href="../../../../res/echarts/eleTree.css" rel="stylesheet"/>
    <script src="../../../../res/echarts/eleTree.js"></script>
    <script src="../../../../res/js/login_common.js" charset="utf-8"></script>
    <style type="text/css">
        body {
            padding: 10px;
        }
    </style>
</head>
<body>
<form class="layui-form layui-form-pane" action="">
    <input type="hidden" id="pkFlowId" name="pkFlowId">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">流程名称</label>
            <div class="layui-input-inline">
                <input type="text" name="flowContent" id="flowContent" lay-verify="required" placeholder="请输入流程名称（必填）"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">所属流程</label>
            <div class="layui-input-inline">
                <input type="text" name="flowLevel" id="flowLevel" lay-verify="required" placeholder="请输入所属流程（必填）"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">权限</label>
            <div class="layui-input-inline">
                <!--<input type="text" name="flowKey" id="flowKey" lay-verify="required" placeholder="请输入关键字（必填）"
                       autocomplete="off"
                       class="layui-input">-->
                <select id="flowKey" name="flowKey">
                    <option value="">请选择对应权限</option>
                    <option value="1">研发</option>
                    <option value="2">打酱油</option>
                    <option value="3">测试</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">审核人员</label>
        <div class="layui-input-block">
            <div id="pkJurisdictionId" class="xm-select-demo" ></div>`
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1" id="sub">立即提交</button>
        </div>
    </div>
</form>
<script>
    var houses;
    let flowKey;
    layui.use(['form', 'layedit', 'laydate', 'layer'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        const pkFlow = getQueryString("pkFlowId");
        const show=getQueryString("show");

        let res=getAjaxById(getRootPath()+"OaPersonnelAction/getButton");
        $("#flowKey").html('<option value="">请选择对应权限</option>');
        $.each(res,function (index,item) {
            $("#flowKey").append('<option value="'+item.jurisdictionCode+'">'+item.jurisdictionName+'</option>');
        });

        //监听提交
        form.on('submit(demo1)', function (data) {
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            let res=postAjax(getRootPath()+"OaFlowAction/addOrUpdate",data.field);
            if(res.code===1){
                parent.layui.table.reload('test');//刷新父页面
                layer.msg("操作成功", {icon: 1});
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            }else{
                layer.msg("操作失败");
            }
            return false;
        });

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
            alert(JSON.stringify(data));
        });

        //加载组件
        layui.config({
            //引入扩展文件库
            base: getRootPath()+'res/layui/dist/'
        }).extend({
            //引入对应的文件名
            xmSelect: 'xm-select'
            //拿到对应的文件模块
        }).use(['xmSelect'], function(){
            //对于模块赋值使用
            var xmSelect = layui.xmSelect;
            //渲染多选
            pkJurisdictionId = xmSelect.render({
                el: '#pkJurisdictionId',
                tree: {
                    show: true,
                    expandedKeys: true,
                },
                height: 'auto',
                data: loadJurisdictions(pkFlow)
            })
        });

        if(pkFlow){
            $("#pkFlowId").val(pkFlow);
            let res=getAjaxById(getRootPath()+'OaFlowAction/findById/'+pkFlow);
            if(res){
                $("#flowContent").val(res.flowContent);
                $("#flowLevel").val(res.flowLevel);
                $("#flowKey").val(res.flowKey);
                flowKey=res.flowKey;
            }
            if(show){
                $("#sub").hide();
            }
        }

        function loadJurisdictions(id){
            let url=getRootPath()+"OaPersonnelAction/findTreeEntryCheckPersons";
            let data={
                wordKey:flowKey
            };
            if(id){
                data={
                    wordKey:flowKey,
                    flowId:id
                };
            }
            let res=getAjax(url,data);
            if(res){
                return res;
            }else{
                return [];
            }
        }

        $("#flowKey").blur(function () {
            flowKey=$("#flowKey").val();
            //加载组件
            layui.config({
                //引入扩展文件库
                base: getRootPath()+'res/layui/dist/'
            }).extend({
                //引入对应的文件名
                xmSelect: 'xm-select'
                //拿到对应的文件模块
            }).use(['xmSelect'], function(){
                //对于模块赋值使用
                var xmSelect = layui.xmSelect;
                //渲染多选
                pkJurisdictionId = xmSelect.render({
                    el: '#pkJurisdictionId',
                    tree: {
                        show: true,
                        expandedKeys: true,
                    },
                    height: 'auto',
                    data: loadJurisdictions($("#pkFlowId").val())
                })
            });

        });


        //重新渲染表单函数
        function renderForm() {
            layui.use('form', function () {
                var form = layui.form(); //高版本建议把括号去掉，有的低版本，需要加()
                form.render();
            });
        }

        form.render();
    });

</script>
</body>
</html>